package com.qulai.amazon_us.service.ql.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qulai.amazon_us.domain.basic.Product;
import com.qulai.common.utils.DateUtils;
import com.qulai.common.utils.http.HttpUtils;
import io.lettuce.core.ScriptOutputType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qulai.amazon_us.mapper.ql.QlShopMapper;
import com.qulai.amazon_us.domain.ql.QlShop;
import com.qulai.amazon_us.service.ql.IQlShopService;

import static com.qulai.common.utils.SecurityUtils.getUsername;
import static com.qulai.common.utils.http.HttpUtils.*;

/**
 * 去来店铺Service业务层处理
 * 
 * @author hunan
 * @date 2025-07-17
 */
@Service
public class QlShopServiceImpl implements IQlShopService 
{
    @Autowired
    private QlShopMapper qlShopMapper;

    /**
     * 查询去来店铺
     * 
     * @param id 去来店铺主键
     * @return 去来店铺
     */
    @Override
    public QlShop selectQlShopById(Long id)
    {
        return qlShopMapper.selectQlShopById(id);
    }

    /**
     * 查询去来店铺列表
     * 
     * @param qlShop 去来店铺
     * @return 去来店铺
     */
    @Override
    public List<QlShop> selectQlShopList(QlShop qlShop)
    {
        return qlShopMapper.selectQlShopList(qlShop);
    }

    /**
     * 新增去来店铺
     * 
     * @param qlShop 去来店铺
     * @return 结果
     */
    @Override
    public int insertQlShop(QlShop qlShop)
    {

        int count = 0;
        try {

            String shopName  = qlShop.getShopName().trim();

            qlShop.setCreateTime(DateUtils.getNowDate());
            qlShop.setShopName(shopName);

            count = qlShopMapper.insertQlShop(qlShop);

            if (count > 0) {
                Map<String, Object> params = new HashMap<>();
                params.put("shop_name", shopName);

                sendPostToPython(shop_add, params);
            }


        } catch (Exception e) {
            System.out.println(e);
        }

        return count;
    }

    /**
     * 修改去来店铺
     * 
     * @param qlShop 去来店铺
     * @return 结果
     */
    @Override
    public int updateQlShop(QlShop qlShop)
    {

        int count = 0;
        try {

            QlShop qlShopOld = qlShopMapper.selectQlShopById(qlShop.getId());
            Long oldStatus = qlShopOld.getStatus();

            String shopName  = qlShop.getShopName().trim();


            qlShop.setShopName(shopName);
            qlShop.setUpdateTime(DateUtils.getNowDate());
            count = qlShopMapper.updateQlShop(qlShop);

            //上架
            if(count>0 && (oldStatus == 0 && qlShop.getStatus() == 1)){

                Map<String, Object> params = new HashMap<>();
                params.put("shop_name", shopName);

                sendPostToPython(shop_online, params);

            }

        } catch (Exception e) {
            System.out.println(e);
        }

        return count;
    }

    /**
     * 批量删除去来店铺
     * 
     * @param ids 需要删除的去来店铺主键
     * @return 结果
     */
    @Override
    public int deleteQlShopByIds(Long[] ids)
    {
        return qlShopMapper.deleteQlShopByIds(ids);
    }

    /**
     * 删除去来店铺信息
     * 
     * @param id 去来店铺主键
     * @return 结果
     */
    @Override
    public int deleteQlShopById(Long id)
    {
        return qlShopMapper.deleteQlShopById(id);
    }
}
